package com.flexcloudsdk.managers;

import java.util.logging.Logger;

import com.flexcloudsdk.mail.CloudMailService;
import com.google.appengine.api.LifecycleManager;

public class CloudLifecycleManager {
	private static final Logger log = Logger.getLogger(CloudLifecycleManager.class.getName());
	private LifecycleManager cloudLifecycleManager;
	private static final long serialVersionUID = 1L;
	private String sourceMethod;
	private String sourceClass;

	public CloudLifecycleManager() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		sourceClass = CloudMailService.class.getName();
		
		cloudLifecycleManager = LifecycleManager.getInstance();
		log.info("CloudLifecycleManager has veen created by BlazeDS");
	}
	
	public void beginShutdown(long deadline) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		cloudLifecycleManager.beginShutdown(deadline);
	}

	public long getRemainingShutdownTime() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		return cloudLifecycleManager.getRemainingShutdownTime();
	}
	
	public void interruptAllRequests() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		cloudLifecycleManager.interruptAllRequests();
	}
	
	public boolean isShuttingDown() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		return cloudLifecycleManager.isShuttingDown();
	}
	
}